document.addEventListener('DOMContentLoaded', function () {
    const jiantou = document.getElementById('jiantou');
    const nav = document.getElementById('nav');
    const menu = document.getElementById('menu-toggle');
    const mobile = document.getElementById('mobile');

    window.onscroll = function () {
        if (document.documentElement.scrollTop > 1) {
            if (jiantou) jiantou.style.display = 'block';
            nav.classList.add('scrolled');
        } else {
            if (jiantou) jiantou.style.display = 'none';
            if (!mobile.classList.contains('show')) {
                nav.classList.remove('scrolled');
            }
        }
    };

    if (jiantou) {
        jiantou.onclick = function () {
            window.scrollTo({ top: 0, behavior: 'smooth' });
        };
    }

    menu.addEventListener('click', function (e) {
        e.stopPropagation();
        const isOpen = mobile.classList.toggle('show');
        menu.classList.toggle('active');

        if (isOpen) {
            nav.classList.add('scrolled');
        } else {
            if (document.documentElement.scrollTop <= 1) {
                nav.classList.remove('scrolled');
            }
        }
    });

    document.addEventListener('click', function (e) {
        if (!mobile.contains(e.target) && e.target !== menu) {
            if (mobile.classList.contains('show')) {
                mobile.classList.remove('show');
                menu.classList.remove('active');
                if (document.documentElement.scrollTop <= 1) {
                    nav.classList.remove('scrolled');
                }
            }
        }
    });
});
